package com.acme.exchange.service;

import java.nio.channels.SelectionKey;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import com.acme.exchange.domain.Exchange;
import com.acme.exchange.domain.UserDataBasa;
import com.acme.exchange.network.proto.DefinedMessage;
import com.acme.exchange.network.server.Server;

public class ExchangeApplication {
	private BlockingQueue<DefinedMessage> q;
	private LinkedList<UserDataBasa> tableOfUsers;
	private Set<String> userLogins;
	private HashSet<SelectionKey> keys;
	
	public ExchangeApplication() {
		q = new LinkedBlockingQueue<DefinedMessage>();
		tableOfUsers = new LinkedList<UserDataBasa>();
		userLogins = new HashSet<String>();
		keys = new HashSet<SelectionKey>();
	}
	
	public void serve() {
		Exchange exchange = Exchange.getExchange();
		Server s = new Server(q, keys);
		ClientMessageProcessor cmp1 = new ClientMessageProcessor(q, tableOfUsers, userLogins, keys, exchange.getExchangeOpenBooks());
		//ClientMessageProcessor cmp2 = new ClientMessageProcessor(q, tableOfUsers,userLogins, keys, exchange.getExchangeOpenBooks());
		
		new Thread(s).start();
		new Thread(cmp1).start();
		//new Thread(cmp2).start();
	}
	
	public static void main(String[] args) {
		ExchangeApplication ea = new ExchangeApplication();
		ea.serve();
	}

}
